package com.mall.admin.service.goods;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.mall.common.annotation.Bean;
import com.mall.common.exception.BusinessExecption;
import com.mall.common.model.goods.GoodsBrand;
import com.mall.common.model.goods.GoodsInfo;
import com.mall.common.model.goods.GoodsInfoProperty;
import com.mall.common.model.goods.GoodsInfoPropertyValue;
import com.mall.common.model.goods.GoodsProperty;
import com.mall.common.model.goods.GoodsPropertyValue;
import com.mall.common.model.goods.GoodsUnit;
import com.mall.common.service.base.BaseService;
import com.mall.common.service.base.DefaultResult;
import com.mall.common.service.base.Result;
import com.mall.common.util.ResultCode;
import com.mall.common.util.StrKit;
import net.sf.json.JSONArray;

/**
 * 商品信息业务层
 * @author taoge
 * @version 1.0
 * @create_at 2017年8月27日下午12:15:58
 */
@Bean
public class GoodsInfoService extends BaseService {
	
	private static final Log LOG = Log.getLog(GoodsInfoService.class);
	
	
	/**
	 * 货品列表
	 * @param condition
	 * @return
	 */
	public Result list(Kv condition) {
		Result result = new DefaultResult();
		try {
			StringBuilder context = new StringBuilder(" from goods_info where 1 = 1");
			Page<GoodsInfo> page = GoodsInfo.dao.paginate(condition.getInt("pageNumber"), condition.getInt("pageSize"), "select *", context.toString());
			result.setDefaultModel(page);
		} catch(Exception e) {
			LOG.error("获取数据异常", e);
		}
		return result;
	}
	
	/**
	 * 添加商品
	 * @return
	 */
	public Result addGoods() {
		Result result = new DefaultResult();
		try {
			List<GoodsBrand> brands = GoodsBrand.dao.find("select * from goods_brand");
			result.setModel("brands", brands);
			List<GoodsUnit> units = GoodsUnit.dao.find("select * from goods_unit");
			result.setModel("units", units);
			List<GoodsProperty> propertys = GoodsProperty.dao.find("select id,name from goods_property");
			result.setModel("propertys", propertys);
		} catch(Exception e) {
			LOG.error("获取数据异常", e);
		}
		return result;
	}
	
	/**
	 * 添加商品
	 * @return
	 */
	@Before(Tx.class)
	public Result create(GoodsInfo goodsInfo, Kv params){
		Result result = new DefaultResult();
		ResultCode resultCode = new ResultCode(ResultCode.SUCCESS, "添加商品成功");
		try {
			//保存商品信息
			goodsInfo.set("created_at", new Date());
			goodsInfo.set("updated_at", new Date());
			goodsInfo.save();
			
			//保存商品属性信息
			String propertyId = params.getStr("propertyId");
			String[] salePrices = (String[]) params.get("salePrices");
	    	String[] stocks = (String[]) params.get("stocks");
//	    	String[] images = (String[]) params.get("images");
			if (StrKit.isNotEmpty(propertyId)) { 
				String[] propertyIds = StrKit.spilt(propertyId);
				for (String id : propertyIds) {
					//保存商品属性信息
					GoodsInfoProperty goodsInfoProperty = new GoodsInfoProperty();
					goodsInfoProperty.set("goods_info_id", goodsInfo.getInt("id"));
					goodsInfoProperty.set("goods_property_id", id);
					goodsInfoProperty.save();
				}
			}
			
	    	
			if (params.get("groupGoods") != null) {
				String[] groupGoods = (String[]) params.get("groupGoods");
				if (groupGoods.length > 0) {
					for (int i = 0; i < groupGoods.length; i++) {
						GoodsInfoPropertyValue goodsInfoPropertyValue = new GoodsInfoPropertyValue();
						String groupValue = groupGoods[i];
						if (StrKit.isNotEmpty(groupValue) && groupValue.contains(",")) {
							String array[] = groupValue.split(",");
							String newValue = "";
							for (String value : array) {
								newValue += value.substring(1, value.length() - 1);
							}
							goodsInfoPropertyValue.set("goods_property_group_value", newValue.substring(0, newValue.length() - 1));
							goodsInfoPropertyValue.set("sale_price", salePrices[i]);
							goodsInfoPropertyValue.set("stock", stocks[i]);
							goodsInfoPropertyValue.set("goods_info_id", goodsInfo.getInt("id"));
							goodsInfoPropertyValue.save();
						}
					}
				}
			}
			result.setResultCode(resultCode);
			return result;
		} catch (Exception e) {
			LOG.error("添加商品失败", e);
			throw new BusinessExecption(new ResultCode(ResultCode.FAIL, "添加商品失败"));
		}
	}
}
